/*
 * The MIT License (MIT)
 *
 * Copyright (c) 2014-2016 abel533@gmail.com
 *
 * Permission is hereby granted, free of charge, to any person obtaining a copy
 * of this software and associated documentation files (the "Software"), to deal
 * in the Software without restriction, including without limitation the rights
 * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
 * copies of the Software, and to permit persons to whom the Software is
 * furnished to do so, subject to the following conditions:
 *
 * The above copyright notice and this permission notice shall be included in
 * all copies or substantial portions of the Software.
 *
 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
 * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
 * THE SOFTWARE.
 */

package com.wf.controller;

import com.github.pagehelper.PageInfo;
import com.wf.entity.Menu;
import com.wf.service.MenuService;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.*;

import java.util.List;


@RestController
@RequestMapping("/menu")
public class MenuController {

    @Autowired
    private MenuService menuService;
    
    @ApiOperation(value="获取菜单分页列表", notes="根据Menu获取菜单列表")
    @ApiImplicitParam(name = "menu", value = "Menu", required = false, dataType = "Menu")
    @CrossOrigin
    @RequestMapping
    public PageInfo<Menu> getAllByPage(@ModelAttribute(value = "menu") Menu menu) {
        List<Menu> countryList = menuService.getAllByPage(menu);
        return new PageInfo<>(countryList);
    }

    @ApiOperation(value="获取菜单", notes="根据菜单id获取菜单")
    @ApiImplicitParam(name = "id", value = "菜单id", required = true, dataType = "String")
    @CrossOrigin
    @RequestMapping(value = "/get/{id}")
    public Menu get(@PathVariable(value = "id") String id) {
        return menuService.getById(id);
    }

    @ApiOperation(value="删除菜单", notes="根据菜单id删除菜单")
    @ApiImplicitParam(name = "id", value = "菜单id", required = true, dataType = "String")
    @CrossOrigin
    @RequestMapping(value = "/delete/{id}")
    public ModelMap delete(@PathVariable(value = "id")  String id) {
        ModelMap result = new ModelMap();
        menuService.deleteById(id);
        result.put("msg", "删除成功!");
        return result;
    }

    @ApiOperation(value="保存（添加/修改）菜单", notes="根据Menu创建菜单")
    @ApiImplicitParam(name = "menu", value = "菜单实体Menu", required = true, dataType = "Menu")
    @CrossOrigin
    @RequestMapping(value = "/save", method = RequestMethod.POST)
    public ModelMap save(@ModelAttribute(value = "menu") Menu menu) {
        ModelMap result = new ModelMap();
        String msg = menu.getId() == null ? "新增成功!" : "更新成功!";
        menuService.save(menu);
        result.put("menu", menu);
        result.put("msg", msg);
        return result;
    }
    @ApiOperation(value="获取用户菜单", notes="根据用户id获取用户菜单")
    @ApiImplicitParam(name = "id", value = "用户id", required = true, dataType = "String")
    @CrossOrigin
    @RequestMapping(value = "/getMenusByUserId/{userId}")
    public List<Menu> getMenusByUserId(@PathVariable(value = "userId") String userId) {
        return menuService.getMenusByUserId(userId);
    }

    @ApiOperation(value="获取角色菜单", notes="根据角色id获取用户菜单")
    @ApiImplicitParam(name = "roleId", value = "角色id", required = true, dataType = "String")
    @CrossOrigin
    @RequestMapping(value = "/getMenusByRoleId/{roleId}")
    public List<Menu> getMenusByRoleId(@PathVariable(value = "roleId") String roleId) {
        return menuService.getMenusByRoleId(roleId);
    }

    @ApiOperation(value="获取用户菜单JSON", notes="根据用户id获取用户菜单JSON")
    @ApiImplicitParam(name = "id", value = "用户id", required = true, dataType = "String")
    @CrossOrigin
    @RequestMapping(value = "/getMenusJsonByUserId/{userId}")
    public String getMenusJsonByUserId(@PathVariable(value = "userId") String userId) {
        return menuService.getAllMenuByUser(userId);
    }

    @ApiOperation(value="获取用户菜单JSON", notes="根据用户id获取用户菜单JSON")
    @CrossOrigin
    @RequestMapping(value = "/getMenusJson")
    public String getMenusJson() {
        return menuService.getAllMenu();
    }
}
